package WebSearch;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.HttpStatusException;

import CommonClass.IMG;
import CommonClass.URLSearch;
import CommonClass.URLlink;
import CommonClass.keyword;



import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.*;
public class WebSearchV1 {
	String uRL;
	Document doc ;
	Elements links;
	Elements media;
	
	public String formatDomain(String domain)
	{
		Pattern httpPattern = Pattern.compile("^https?\\://");
        Pattern partialPattern = Pattern.compile("www\\.[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?");
        Pattern lpartialPattern = Pattern.compile("[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?");
        Pattern ipPattern = Pattern.compile("([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}");
        Pattern fullipPattern = Pattern.compile("^https?\\://([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}");
        Matcher ipMatcher = fullipPattern.matcher(domain);
        Pattern fullURLpattern = Pattern.compile("^https?\\://(www\\.)?[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?");
        Matcher matcher = fullURLpattern.matcher(domain);
        
        if(httpPattern.matcher(domain).find())
        {
        	return domain;
        }
        if(matcher.matches())
        {
        	//URL == http://www.google.com
        	return domain;
        }
        else if(partialPattern.matcher(domain).find())
        {
        	// URL == www.google.com
        	String uRLStr = "http://" + domain;
        	// Check with full URL again
        	if(matcher.reset(uRLStr).matches())
        		return uRLStr;
        	else
        		return null;
        }

        
        
        if(ipMatcher.matches())
        {
        	return domain;
        }
        else if(ipPattern.matcher(domain).find())
        {
        	String uRLStr = "http://" + domain;
        	if(ipMatcher.reset(uRLStr).find())
        		return uRLStr;
        	else
        		return null;
        }
        else if(lpartialPattern.matcher(domain).find())
        {
        	// URL == "google.com"
        	String uRLStr = "http://" + domain;
        	// Check with full URL again
        	if(matcher.reset(uRLStr).matches())
        		return uRLStr;
        	else
        		return null;
        }
		return domain;

	}
	
	public String formatURL(String domain, String uRL)
	{
        Pattern fullURLpattern = Pattern.compile("^https?\\://www\\.[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?");
        Matcher matcher = fullURLpattern.matcher(uRL);
        
        if(matcher.matches())
        {
        	return uRL;
        }
        else
        {
        	String uRLStr = domain + "/" + uRL ;
        	if(matcher.reset(uRLStr).matches())
        	{
        		return uRLStr;
        	}
        	else
        	{
        		return null;
        	}
        }
	}
	
	public ArrayList<URLlink> getHREFs() throws Exception
	{
		// System.out.println("Fetching " + uRL + " ...");
		 ArrayList<URLlink> listOfURL = new ArrayList<URLlink>();
		 links = doc.body().select("a[href]");
      // Elements media = doc.body().select("[src]");
		ArrayList<keyword> keywords;
		//System.out.printf("\nLinks: (%d)", links.size());
		for (Element link : links) 
		{
			 if(!link.text().isEmpty() || !link.attr("alt").isEmpty() || !link.attr("title").isEmpty())
			 {
				 
				 keywords = new ArrayList<keyword>();
				 if(!link.text().isEmpty())
				 {
					 keywords.add(new keyword(link.attr("abs:href"), "TEXT", link.text()));
				 }
				 if(!link.attr("alt").isEmpty())
				 {
					 keywords.add(new keyword(link.attr("abs:href"), "ALT", link.attr("alt")));
				 }
				 if(!link.attr("title").isEmpty())
				 {
					 keywords.add(new keyword(link.attr("abs:href"), "TITLE", link.attr("title")));
				 }
			 }
			 else
			 {
				 keywords = null;
			 }
			 listOfURL.add(new URLlink(uRL, link.attr("abs:href"), keywords));
		}
			//System.out.printf(" * a: <%s>  (%s) %s %s \n", link.attr("abs:href"), link.text(), link.attr("abs:alt"), link.attr("abs:title"));
		 
		 return listOfURL;
	} 
	
	
	public ArrayList<IMG> getIMGs() throws Exception
	{
//		 System.out.println("Fetching " + uRL + " ...");
		 ArrayList<IMG> listOfURL = new ArrayList<IMG>();
		 
	 //	 links = doc.body().select("a[href]");
        media = doc.body().select("[src]");
		ArrayList<keyword> keywords;
		//System.out.printf("\nLinks: (%d)", links.size());
		for (Element src : media) 
		{
			if (src.tagName().equals("img"))
			{
				 if(!src.attr("alt").isEmpty() || !src.attr("title").isEmpty())
				 {
					 
					 keywords = new ArrayList<keyword>();
					 if(!src.attr("alt").isEmpty())
					 {
						 keywords.add(new keyword(src.attr("abs:src"), "ALT", src.attr("alt")));
					 }
					 if(!src.attr("abs:title").isEmpty())
					 {
						 keywords.add(new keyword(src.attr("abs:src"), "TITLE", src.attr("title")));
					 }
				 }
				 else
				 {
					 keywords = null;
				 }
				 listOfURL.add(new IMG(uRL, src.attr("abs:src"), 0, 0, keywords));
			}
		}
			//System.out.printf(" * a: <%s>  (%s) %s %s \n", link.attr("abs:href"), link.text(), link.attr("abs:alt"), link.attr("abs:title"));
		 
		 return listOfURL;
	} 

	public ArrayList<IMG> getIMGs(String[] keyword, int size) throws Exception
	{
//		 System.out.println("Fetching " + uRL + " ...");
		 ArrayList<IMG> listOfURL = new ArrayList<IMG>();
		 
	 //	 links = doc.body().select("a[href]");
        media = doc.body().select("[src]");
		ArrayList<keyword> keywords = null;
		//System.out.printf("\nLinks: (%d)", links.size());
		for (Element src : media) 
		{
			if (src.tagName().equals("img"))
			{
				 if(!src.attr("alt").isEmpty() || !src.attr("title").isEmpty())
				 {
					 
					 
					 if(!src.attr("alt").isEmpty())
					 {	
						 
						 for(int i = 0; i < size; i++)
						 {
							 if(src.attr("alt").contains(keyword[i]))
							 {
								 if(keywords != null)
								 {
									 keywords = new ArrayList<keyword>();
								 }
								 keywords.add(new keyword(src.attr("abs:src"), "ALT", src.attr("alt")));
								 break;
							 }
						 }
					 }
					 if(!src.attr("abs:title").isEmpty())
					 {
						 for(int i = 0; i < size; i++)
						 {
							 if(src.attr("title").contains(keyword[i]))
							 {
								 if(keywords != null)
								 {
									 keywords = new ArrayList<keyword>();
								 }
								 keywords.add(new keyword(src.attr("abs:src"), "TITLE", src.attr("title")));
								 break;
							 }
						 }
					 }
				 }
				 else
				 {
					 keywords = null;
				 }
				 
				 for(int i = 0; i < size; i++)
				 {
					 if(src.attr("abs:src").contains(keyword[i]))
					 {
						 listOfURL.add(new IMG(uRL, src.attr("abs:src"), 0, 0, keywords));
						 break;
					 }
				 }
			}
		}
			//System.out.printf(" * a: <%s>  (%s) %s %s \n", link.attr("abs:href"), link.text(), link.attr("abs:alt"), link.attr("abs:title"));
		 
		 return listOfURL;
	} 
	public URLSearch fetchWebPageWithKeyword(String uRL, String keyword)
	{
		 //Validate.isTrue(uRL == null || uRL.length() == 0, "usage: supply url to fetch");
		// Validate.isFalse(checkURL(uRL), "usage: supply a URL with in the format http://www.example.com");
		 System.out.println("Fetching " + uRL + " ...");
		 this.uRL = uRL;
		 try {
			doc= Jsoup.connect(uRL).get();
			
			String[] keyword_array = keyword.split(" ");

				return new URLSearch(uRL, getHREFs(), getIMGs(keyword_array, keyword_array.length));

//	        links = doc.body().select("a[href]");
//	        media = doc.body().select("img[src]");
//	        
//	        System.out.printf("\nMedia: (%d)", media.size());
//	        for (Element src : media) {
//	            if (src.tagName().equals("img"))
//	            	System.out.printf(" * %s: <%s> %sx%s (%s)\n",
//	                        src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
//	                        src.attr("alt"));
//	            else
//	            	System.out.printf(" * %s: <%s>\n", src.tagName(), src.attr("abs:src"));
//	        }

//	        System.out.printf("\nLinks: (%d)", links.size());
//	        for (Element link : links) {
//	        	System.out.printf(" * a: <%s>  (%s) (%s) (%s) \n", link.attr("abs:href"), link.text(), link.attr("alt"), link.attr("title"));
//	        	//System.out.printf(" * a: <%s>  (%s) \n", link.attr("abs:href"), link.text());
//	        }
//			if (links != null)
//			{
//				links.clear();
//				media.clear();
//			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public URLSearch fetchWebPage(String uRL) throws SearchException
	{
		 //Validate.isTrue(uRL == null || uRL.length() == 0, "usage: supply url to fetch");
		// Validate.isFalse(checkURL(uRL), "usage: supply a URL with in the format http://www.example.com");
		 System.out.println("Fetching " + uRL + " ...");
		 this.uRL = uRL;
		 try {
			doc= Jsoup.connect(uRL).get();
			

				return new URLSearch(uRL, getHREFs(), getIMGs());
		
		} catch (HttpStatusException e) {
			System.out.println((e.getUrl().toString()));
			e.printStackTrace();
			
			throw new SearchException(e.getStatusCode());
		}
		 catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println((e.getStackTrace())[0].toString());
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
}
